package ace.tool.generate.code.impl.resolver.scheme.resolver.core;

import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.EntityClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.ManagerImplClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ClassFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ProjectInfo;
import ace.tool.generate.code.impl.resolver.scheme.resolver.ClassFileInfoResolver;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import java.util.List;
import lombok.RequiredArgsConstructor;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@RequiredArgsConstructor
public class ManagerImplClassInfoResolver {

  private final GenerateInput input;
  private final GenerateModelInput modelInput;
  private final List<EntityClassInfo> entityClassInfos;
  private final ProjectInfo coreProjectInfo;
  private final CommonInfo commonInfo;

  public ManagerImplClassInfo resolve() {

    String entityModelId = EntityModelManager.getInstance()
        .getEntityModelId(input, modelInput);

    EntityClassInfo entityClassInfo = EntityModelManager.getInstance()
        .getEntityClassInfoByEntityModelId(this.entityClassInfos, entityModelId);

    ClassFileInfo classFileInfo =
        ClassFileInfoResolver.builder()
            .className(entityClassInfo.getModel().getClassName())
            .classNameSuffix(input.getManagerImplClassNameSuffix())
            .classPackagePrefix(input.getManagerImplClassPackagePrefix())
            .rootPackageFullName(commonInfo.getRootPackageFullName())
            .sourceCodeRootDir(coreProjectInfo.getSourceCodeRootDir())
            .build()
            .resolve();

    return ManagerImplClassInfo.builder()
        .entityModelId(entityClassInfo.getEntityModelId())
        .info(classFileInfo)
        .extendClassName(this.getExtendClassName())
        .build();
  }

  private String getExtendClassName() {
    if (modelInput.getFrameworkBizAppFlag()) {
      return "AbstractApplicationManager";
    }
    if (modelInput.getFrameworkBizEntityOfAppFlag()) {
      return "AbstractEntityOfAppManager";
    }
    return "AbstractSpringDtoManager";
  }
}
